package com.leyou.mail.mq;

import com.leyou.mail.utils.MailUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Map;

@Component
public class MailListener {

    @Autowired
    private MailUtils mailUtils;



    /**
     * 发送邮箱验证码
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "mail.verify.code.queue", durable = "true"),
            exchange = @Exchange(
                    value = "ly.mail.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {"mail.verify.code"}))
    public void listenSendMail(Map<String,String> msg) {

        if (CollectionUtils.isEmpty(msg)) {
            return;
        }
        if (StringUtils.isBlank(msg.get("mail"))){
            return;
        }
        //处理消息，发邮箱验证码给用户
        mailUtils.sendMail(msg.get("mail"),"验证码为"+msg.get("code")+"5分钟有效","leyou商城验证码");

    }





}
